/**********************************************************************************
 * cxc demo
 * All rights reserved.
 * @author:chenxiaocang
 * 2013-4-20 下午2:18:39
 *@description:
**********************************************************************************/
package com.cxc.myweb.demo.model.bpo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.cxc.myweb.demo.model.dao.UserMapper;
import com.cxc.myweb.demo.model.dto.User;

public class UserBpo {

	private UserMapper userMapper;
	
	private final Logger logger = Logger.getLogger(UserBpo.class);

	private final boolean hasDB = true;
	
	private String message;
	
	private static List<User> userTempList;
	static {
		userTempList  = new ArrayList<User>();
		for(int i=0; i<3;i++ ){
			User usr = new User();
			usr.setId(i+1);
			usr.setName("tom"+String.valueOf(i+1));
			usr.setPassword("tom"+String.valueOf(i+1));
			userTempList.add(usr);
		}
	}
	
	public int counts() {
		return userMapper.counts();
	}
	
	public User queryUserByPk(int userId) {
		return userMapper.selectByPrimaryKey(userId);
	}
	
	public List<User> queryUsers(User user) {
		if ( user == null ){
			User record = new User();
			record.setId(null);
			record.setName(null);
			record.setPassword(null);
			return userMapper.selectSelective(record);			
		}
		return userMapper.selectSelective(user);
	}
	
	public int insertUser(User record) {
		return userMapper.insert(record);
	}
		
	public void insertUsers(List<User> userList) {
		userMapper.insertBatch(userList);
		throw new RuntimeException("抛出测试异常，事务插入记录回滚测出.");
		//警告：此处抛出的异常，不能在本类捕获，否则不会回滚，到没有被事务控制的层捕获			
	}

	public List<User> queryUserLogs(User user) {
		return userMapper.selectUserLogs(user);
	}

	public List<User> queryUsersRoles() {
		return userMapper.selectUsersRoles();
	}

	public void callProc1(Map para) {
		userMapper.callProc1(para);
	}

	public List<User> callProc2(Map para) {
		return userMapper.callProc2(para);
	}

	public int validateUser(String name,String password) {
		if ( name==null || password==null ) {
			this.message="输入参数错误.";
			return -1;
		}
		for (User user:userTempList ){
			if ( user.getName().equals(name)&& user.getPassword().equals(password)) {
				this.message="验证成功.";
				return 0;
			}
		}
		this.message="用户名称或密码错误.";
		return -2;
	}
	
	public UserMapper getUserMapper() {
		return userMapper;
	}

	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}	
	
	
}
